<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>{:config("app.app_name")}</title>
    <meta name="keywords" content="">
    <meta name="description" content="">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link href="__STATIC__/layui/css/layui.css" rel="stylesheet">
    <link rel="stylesheet" href="__STATIC__/css/layuimini.css?v=2.0.4.2" media="all">
    <link rel="stylesheet" href="__STATIC__/css/public.css?v=2.0.4.3" media="all">
    <link rel="stylesheet" href="__STATIC__/css/themes/default.css" media="all">
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>

    </style>
</head>
<body  >
<div class="layuimini-container">
    <div class="layuimini-main">
        <table class="layui-hide" id="ID-treeTable-demo"></table>
        <script type="text/html" id="TPL-treeTable-demo">
             <div class="layui-btn-container">
                   <button class="layui-btn layui-btn-sm" lay-event="getChecked">授权</button>
             </div>

        </script>

        <script type="text/html" id="status">
            {{# if(d.status === 1){ }}
            <span class="layui-badge layui-bg-green">显示</span>
            {{# } else { }}
            <span class="layui-badge">隐藏</span>
            {{# } }}
        </script>
        <script type="text/html" id="isParent">
            {{# if(d.isParent === 1){ }}
            <span class="layui-badge layui-bg-blue">菜单</span>
            {{# } else { }}
            <span class="layui-badge layui-bg-green">按钮</span>
            {{# } }}
        </script>
    </div>
</div>
<script src="__JS__/jquery.min.js"></script>
<script src="__STATIC__/layui/layui.js"></script>
<script src="__STATIC__/layui/lay-config.js?v=2.0.0" charset="utf-8"></script>
<script src="__JS__/common.js"></script>
<script>

    layui.use( function () {
        var $ = layui.jquery;
        var treeTable = layui.treeTable;
        var layer = layui.layer;
        var dropdown = layui.dropdown;
        // 渲染
        var inst = treeTable.render({
            elem: '#ID-treeTable-demo',
            id:'Menu',
            url: '/admin/AuthGroup/auth?id={$info.id}', // 此处为静态模拟数据，实际使用时需换成真实接口
            tree: {
                customName: {name:"title"},
            },
            maxHeight: '701px',
            toolbar: '#TPL-treeTable-demo',
            cols: [[
                {type: 'checkbox', fixed: 'left'},
                {field: 'id', title: 'ID',  sort: true},
                {field: 'title', title: '名称',},
                {field: 'name', title: '标识符',},
                {field: 'isParent', title: '类型',templet: '#isParent'},
                {field: 'status', title: '状态',templet: '#status'},
            ]],
            page: true,
            done:function(data){
                if(data.data&&data.data.length>0){

                    var res = data.data

                    data.data.map(function(item,index){
                        treeTable.expandNode('Menu', {
                            index: item.LAY_DATA_INDEX, // 第一行
                            expandFlag: true // 展开
                        });
                        setCheck(item)
                    })
                }
            }
        });
        //设置选中项
        function setCheck(item) {
            var roles = "{$info.rules}"
            if (item.children&&item.children.length > 0){
               for(item1 in item.children){
                   setCheck(item.children[item1])
               }
             }else{
                if(roles.indexOf(item.id)!=-1){
                    treeTable.setRowChecked("Menu",  {
                        index: item.LAY_DATA_INDEX, // 第一行
                        checked: true // 展开
                    });
                }

            }
            return;
        }
        // 表头工具栏工具事件
        treeTable.on("toolbar(Menu)", function (obj) {
            var config = obj.config;
            var tableId = config.id;
            var status = treeTable.checkStatus(tableId);
            // 获取选中行
            if (obj.event === "getChecked") {
                if(!status.data.length) return layer.msg('请选择数据');

                var auth_rule_ids = [];
                status.data.forEach(function (item){
                    auth_rule_ids.push(item.id)
                })
            }
            auth_rule_ids = auth_rule_ids.join(",")
            Ajax("/admin/AuthGroup/updateAuthGroupRule?id={$info.id}","post",{"auth_rule_ids":auth_rule_ids},
                success=function (info) {
                    if (info.code === 1) {
                        layer.confirm(info.msg, {icon: 1,btn: ['返回列表'],cancel: function(){
                                window.location.href = ""
                            }}, function(){
                            layer.close(layer.index);
                            window.parent.location.reload();
                        });
                    }else{
                        layer.msg(info.msg)
                    }
                },error=function(e){
                    console.log(e,22)
                }
            );
        });

    });
</script>

</body>
</html>
